import createApp from "./main";
import { renderToString } from "vue/server-renderer";

export const render = (url) => {
  const { app, router } = createApp();
  return new Promise(async (resolve, reject) => {
    try {
      await router.push(url);
      await router.isReady(); // 等待路由准备就绪
      // 在渲染前可以预取数据
      const html = await renderToString(app); // 渲染为字符串
      resolve(html); // 返回渲染结果
    } catch (error) {
      console.error("Error during SSR:", error); // 错误处理
      reject(error); // 返回错误
    }
  });
};
